/*
 *	Yalala
 *	
 *	Copyright © MediaMonks B.V.
 *	All rights reserved.
 *	
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions are met:
 *	1. Redistributions of source code must retain the above copyright
 *	   notice, this list of conditions and the following disclaimer.
 *	2. Redistributions in binary form must reproduce the above copyright
 *	   notice, this list of conditions and the following disclaimer in the
 *	   documentation and/or other materials provided with the distribution.
 *	3. All advertising materials mentioning features or use of this software
 *	   must display the following acknowledgement:
 *	   This product includes software developed by MediaMonks B.V.
 *	4. Neither the name of MediaMonks B.V. nor the
 *	   names of its contributors may be used to endorse or promote products
 *	   derived from this software without specific prior written permission.
 *	
 *	THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
 *	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *	DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
 *	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	
 */

package com.mediamonks.yalala.data.filters 
{
	import flash.utils.Dictionary;
	import nl.acidcats.yalog.common.MessageData;
	import com.mediamonks.yalala.data.vo.StringFilterData;

	/**
	 * @author Thijs Broerse
	 */
	public class StringFilter extends AbstractFilter implements IMessageFilter
	{
		private var _field:String;
		private var _data:StringFilterData;
		
		private var _results:Dictionary;
		
		private var _text:String;
		private var _isRegExp:Boolean;
		private var _regExp:RegExp;

		public function StringFilter(field:String, data:StringFilterData)
		{
			this._field = field;
			this._data = data;
		}

		override public function isValid(message:MessageData):Boolean
		{
			if (!this.enabled || !this._data.text)
			{
				return true;
			}
			
			if (this._results && this._data.isRegExp == this._isRegExp && this._data.text == this._text)
			{
				// filter hasn't changed since last time, first check if we already filtered this message
				if (this._isRegExp.hasOwnProperty(message)) return this._results[message];
			}
			else
			{
				// clear results
				this._results = new Dictionary(true);
				this._text = this._data.text;
				this._isRegExp = this._data.isRegExp;
				
				if (this._isRegExp) this._regExp = new RegExp(this._text);
			}
			
			if (!this._data.isRegExp)
			{
				return this._results[message] = String(message[this._field]).toLowerCase().indexOf(this._data.text.toLowerCase()) != -1;
			}
			else
			{
				return this._results[message] = this._regExp.test(String(message[this._field]));
			}
			return false;
		}
		
		public function get field():String
		{
			return this._field;
		}
		
		public function get data():StringFilterData
		{
			return this._data;
		}
	}
}
